/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package agh.rozproszony.DBStorage;


import Controller.XML.Komparator;
import View.InvitationRow;
import agh.rozproszony.model.Invitation;
import agh.rozproszony.model.Message;
import agh.rozproszony.model.Reservation;
import agh.rozproszony.model.Room;
import agh.rozproszony.model.User;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.util.*;

/**
 *
 * @author Marcin_PC
 */
public class DatabaseUnitTestClass implements DataBaseEngine {

    List<User> users = new ArrayList<User>();
    List<Room> rooms = new ArrayList<Room>();
    List<Reservation> reservations = new ArrayList<Reservation>();
    List<Message> messages = new ArrayList<Message>();
    List<Invitation> invitations = new ArrayList<Invitation>();

    @Override
    public User getUser(int id) throws SQLException {
        for (User user : users) {
            if (user.getId() == id) {
                return user;
            }
        }
        return null;
    }

    @Override
    public Room getRoom(int roomId) throws SQLException {
        for (Room room : rooms) {
            if (room.getId() == roomId) {
                return room;
            }
        }
        return null;
    }

    @Override
    public Reservation getReservation(int id) throws SQLException {
        for (Reservation reservation : reservations) {
            if (reservation.getId() == id) {
                return reservation;
            }
        }
        return null;
    }

    @Override
    public Message getMessage(int id) throws SQLException {
        for (Message message : messages) {
            if (message.getId() == id) {
                return message;
            }
        }
        return null;
    }

    @Override
    public Invitation getInvitation(int id) throws SQLException {
        for (Invitation invitation : invitations) {
            if (invitation.getId() == id) {
                return invitation;
            }
        }
        return null;
    }

    @Override
    public User getUserByLogin(String login) throws SQLException {
        for (User user : users) {
            if (user.getLogin().equals(login)) {
                return user;
            }
        }
        return null;
    }

    @Override
    public User getUserByEmail(String email) throws SQLException {
        for (User user : users) {
            if (user.getEmail().equals(email)) {
                return user;
            }
        }
        return null;
    }

    @Override
    public void changePassword(User user, String newPassword) throws SQLException {
        getUser(user.getId()).setPassword(newPassword);
    }

    @Override
    public int getUserTypeByName(String name) throws SQLException {
        return getUserByLogin(name).getType();

    }

    @Override
    public int getUserTypeByID(int id) throws SQLException {
        return getUser(id).getType();
    }

    @Override
    public Message getMessage(String title) throws SQLException {
        Message message = new Message();

        message.setId(0);
        message.setReaded(0);
        message.setRecipientId(0);
        message.setSenderId(0);
        message.setTitle("Test Title");
        message.setContent("Content txt");

        return message;
    }

    @Override
    public ArrayList<Message> getMessages(int id) throws SQLException {
        Collections.sort(messages, new Komparator());
        return (ArrayList) messages;
    }

    @Override
    public ArrayList<Message> getNotReadedMessages(int userId) throws SQLException {

        ArrayList<Message> messages = new ArrayList<Message>();

        Message message = new Message();

        message.setId(0);
        message.setReaded(0);
        message.setRecipientId(0);
        message.setSenderId(0);
        message.setTitle("Test Title");
        message.setContent("Content txt");

        messages.add(message);

        return messages;
    }

    @Override
    public List<Reservation> getRoomReservations(int roomId, int day, int month, int year) throws SQLException {
        List<Reservation> roomReservations = new ArrayList<Reservation>();
        for (Reservation reservation : reservations) {
            if (reservation.getRoomId() == roomId && reservation.getDay() == day && reservation.getMonth() == month && reservation.getYear() == year) {
                roomReservations.add(reservation);
            }
        }
        return roomReservations;
    }

    @Override
    public void insertUser(User user) throws SQLException {
        users.add(user);
        user.setId(users.size()); // Sztuczne nadawanie kolejnych ID
    }

    @Override
    public void insertMessage(Message message) throws SQLException {
        message.setId(messages.size()); 
        messages.add(message);
        
    }

    @Override
    public void insertReservation(Reservation reservation) throws SQLException {
        reservation.setId(reservations.size()); 
        reservations.add(reservation);
        
    }

    @Override
    public void insertRoom(Room room) throws SQLException {
        room.setId(rooms.size()); 
        rooms.add(room);
        
    }

    @Override
    public void clearDatabase() throws SQLException {
        users.clear();
        rooms.clear();
        reservations.clear();
        invitations.clear();
        messages.clear();
    }

    @Override
    public List<Room> getAllRooms() throws SQLException {
        return rooms;
    }

    @Override
    public List<User> getAllUsers() throws SQLException {
        return users;
    }

    @Override
    public List<Reservation> getAllReservations() throws SQLException {
        return reservations;
    }

    @Override
    public boolean isReservationPossible(Reservation reservation) {

        for (Reservation existingReservation : reservations) 
        {
            // Jezeli rezerwacja jest na ten sam pokoj i dzien...
            if (existingReservation.getRoomId() == reservation.getRoomId()
                && 
                existingReservation.getYear() == reservation.getYear()
                && 
                existingReservation.getMonth() == reservation.getMonth()
                && 
                existingReservation.getDay() == reservation.getDay()) 
            {
                // Jezeli poczatek rezerwacji jest w zajetym przedziale, zwroc false
                if (reservation.getFromHour() >= existingReservation.getFromHour()
                        && 
                    reservation.getFromHour() <= existingReservation.getToHour()) 
                {
                    return false;
                }
                // Jezeli koniec rezerwacji jest w zajetym przedziale, zwroc false
                if (reservation.getToHour() >= existingReservation.getFromHour()
                        && 
                    reservation.getToHour() <= existingReservation.getToHour()) 
                {
                    return false;
                }
                // Jezeli istniejaca rezerwacja jest wewnatrz dodawanej, zwroc false
                if (reservation.getFromHour() < existingReservation.getFromHour()
                        && 
                    reservation.getToHour() > existingReservation.getToHour()) {
                    return false;
                }
            }
        }

        return true;
    }

    @Override
    public void insertInvitiation(Invitation invitation) throws SQLException {
        invitations.add(invitation);
    }

    @Override
    public List<Invitation> getAwaitingInvitations(int userId) throws SQLException {
        List<Invitation> awaitingInviatations = new ArrayList<Invitation>();

        for (Invitation invitation : invitations) {
            if (invitation.getUserId() == userId && !invitation.isAccepted()) {
                awaitingInviatations.add(invitation);
            }
        }

        return awaitingInviatations;
    }

    @Override
    public String getReservationRoomName(int id) throws RemoteException, SQLException {

        for (Room room : rooms) {
            if (this.getReservation(id).getRoomId() == room.getId()) {
                return room.getName();
            }
        }
        throw new RemoteException("Nie znaleziono pokoju dla rezerwacji " + id);
    }

    @Override
    public String getReservationOwnerLogin(int id) throws RemoteException, SQLException {

        for (User user : users) {
            if (this.getReservation(id).getOwnerId() == user.getId()) {
                return user.getEmail();
            }
        }
        throw new RemoteException("Nie znaleziono urzytkownika dla rezerwacji " + id);
    }

    @Override
    public void deleteReservation(Reservation reservation) {
        reservations.remove(reservation);
    }

    @Override
    public void deleteUser(User user) {
        users.remove(user);
    }

    @Override
    public void deleteRoom(Room room) {
        rooms.remove(room);
    }

    @Override
    public List<Reservation> getAllReservations(Date from, Date to) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public List<Reservation> getAllReservations(Date from, Date to, int roomId) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public int getReservationId(Reservation reservation) throws SQLException {
        return reservations.get(reservation.getId()).getId();
    }

    @Override
    public int deleteInvitation(InvitationRow removeUnselectedRows) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public void setInvitationAccepted(int userId, int reservationId) throws SQLException {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }
}
